package com.ass.nacos.service.impl;

import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.ass.nacos.service.NacosApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NacosApiServiceImpl implements NacosApiService {


    @Autowired
    private ConfigService configService;

    @Autowired
    private NamingService namingService;


    @Override
    public String getConfigValue(String dataId, String group, Long timeoutMs) {
        String content =null;
        try {
             content = configService.getConfig(dataId, group, timeoutMs==null?3000:timeoutMs);
        } catch (NacosException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return content;
    }

    @Override
    public boolean publishConfig(String dataId, String group, String content, String type) {
        boolean isPublishOk = false;
        try {
            isPublishOk =  configService.publishConfig(dataId, group, content, type);
        } catch (NacosException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return isPublishOk;
    }

    @Override
    public boolean removeConfig(String dataId, String group) {
        boolean isRemoveOk = false;
        try {
            isRemoveOk =  configService.removeConfig(dataId, group);
        } catch (NacosException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return isRemoveOk;
    }

    //还有好几个方法 注册实例的
    @Override
    public boolean registerInstance(String serviceName, String ip, int port) {
        boolean state = false;
        try {
            namingService.registerInstance(serviceName,ip,port);
            state = true;
        } catch (NacosException e) {
            e.printStackTrace();
        }
        return state;
    }

    @Override
    public boolean registerInstance(String serviceName, Instance instance) {
        boolean state = false;
        try {
            namingService.registerInstance(serviceName,instance);
            state = true;
        } catch (NacosException e) {
            e.printStackTrace();
        }
        return state;
    }


    @Override
    public boolean deregisterInstance(String serviceName, String ip, int port) {
        boolean state = false;
        try {
            namingService.deregisterInstance(serviceName, ip, port, "DEFAULT");
            state = true;
        } catch (NacosException e) {
            e.printStackTrace();
        }
        return state;
    }

    @Override
    public List<Instance> getAllInstances(String serviceName) {
        List<Instance> list = new ArrayList<>();
        try {
            list = namingService.getAllInstances(serviceName);
        } catch (NacosException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Instance> selectInstances(String serviceName, boolean healthy) {
        List<Instance> list = new ArrayList<>();
        try {
            list = namingService.selectInstances(serviceName,healthy);
        } catch (NacosException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Instance selectOneHealthyInstance(String serviceName) {
        Instance instance = new Instance();
        try {
            instance = namingService.selectOneHealthyInstance(serviceName);
        } catch (NacosException e) {
            e.printStackTrace();
        }
        return instance;
    }
}
